package com.ytjj.qmyx.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.exception.ApiException;
import com.ytjj.qmyx.admin.mapper.NewBannersMapper;
import com.ytjj.qmyx.admin.model.NewBanners;
import com.ytjj.qmyx.admin.model.dto.NewBannersDto;
import com.ytjj.qmyx.admin.service.NewBannerService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.util.StringUtil;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class NewBannerServiceImpl implements NewBannerService {

    @Resource
    private NewBannersMapper newBannersMapper;


    @Override
    public int insertBanners(NewBanners newBanners) {
        if (StringUtil.isEmpty(newBanners.getTitle())) {
            throw new ApiException("广告图名称不能为空");
        }
        if (null == newBanners.getType()) {
            throw new ApiException("广告图位置不能为空");
        }
        if (StringUtil.isEmpty(newBanners.getImgUrl())) {
            throw new ApiException("轮播图图片不能为空");
        }
        if (StringUtil.isEmpty(newBanners.getPath())) {
            throw new ApiException("跳转链接不能为空");
        }

        newBanners.setCreateTime(new Date());
        newBanners.setUpdateTime(new Date());
        return newBannersMapper.insertSelective(newBanners);
    }

    @Override
    public CommonPage findBannersAll(NewBannersDto newBannersDto) {
        PageHelper.startPage(newBannersDto.getPageNum(), newBannersDto.getPageSize());
        List<NewBanners> list = newBannersMapper.findBannersAll(newBannersDto);
        return CommonPage.restPage(list);
    }

    @Override
    public int updateBanners(NewBanners newBanners) {
        if (StringUtil.isEmpty(newBanners.getTitle())) {
            throw new ApiException("广告图名称不能为空");
        }
        if (null == newBanners.getType()) {
            throw new ApiException("广告图位置不能为空");
        }
        if (StringUtil.isEmpty(newBanners.getImgUrl())) {
            throw new ApiException("轮播图图片不能为空");
        }

        newBanners.setUpdateTime(new Date());
        return newBannersMapper.updateBannersById(newBanners);
    }
}
